/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.json.database;

import com.json.bean.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 数据库更高一级更改
 *
 * @author json
 */
public class UserDao {

    /**
     *
     * 插入一个用户
     *
     * @param user 用户的POJO类
     * @return 是否成功
     */
    public boolean insert(User user) {
        String sql = "insert into user (username,pass) values("
                + "'" + user.getUsername() + "'"
                + "," + user.getPass()
                + ")";
        DatabaseAccess db = new DatabaseAccess();
        boolean b = db.update(sql);//增
        db.close();
        return b;
    }

    /**
     * 删除某一项
     *
     * @param id
     * @return 如果成功返回true 否则返回false
     */
    public boolean dell(int id) {
        String sql = "delete from user where id=" + id;
        DatabaseAccess db = new DatabaseAccess();
        boolean b = db.update(sql);//删
        db.close();
        return b;
    }

    /**
     *
     * 修改用户的资料
     *
     *
     * @param user
     * @return
     */
    public boolean update(User user) {
        String sql = "update user set "+"username="+"'"+user.getUsername()+"'"+","+"pass="+"'"+user.getPass()+"'"+"where id="+user.getId();
        DatabaseAccess db = new DatabaseAccess();
        boolean b = db.update(sql);//修
        db.close();
        return b;

    }
    
  

    /**
     *
     *
     *
     *
     * 根据用户名获得一个用户
     *
     * @param username 用户名
     * @return 用户的POJO类
     */
    public User getOneUser(String username) {
        String sql = "select * from user where username='" + username + "'";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        User user = new User();
        try {
            if (rs != null && rs.next()) {
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return user;

    }

    /**
     * 根据用户id获得一个用户
     *
     * @param id 用户id
     * @return 用户的POJO类
     */
    public User getOneUser(int id) {
        String sql = "select * from user where id=" + id + "";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        User user = new User();
        try {
            if (rs != null && rs.next()) {
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
                // user.setAdm(rs.getInt("adm"));
                //   user.setAge(rs.getInt("age"));
                user.setId(rs.getInt("id"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return user;
    }

    /**
     *
     * 获得所有用户
     *
     * @return 用户集合
     */
    public LinkedList<User> getUserLisk() {
        String sql = "select * from user";
        DatabaseAccess db = new DatabaseAccess();
        ResultSet rs = db.query(sql);
        LinkedList<User> userlist = new LinkedList<>();
        try {
            while (rs != null && rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPass(rs.getString("pass"));
                //   user.setAdm(rs.getInt("adm"));
                // user.setAge(rs.getInt("age"));
                userlist.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.close();
        return userlist;
    }

    /**
     *
     *
     *
     *
     * 验证登陆
     *
     * @param user 用户表的pojo类
     * @return 登陆成功返回true 其他返回false
     */
    public boolean login(User user) {
        String sql = "select * from user where username='" + user.getUsername() + "' and pass='" + user.getPass() + "'";//这里不要写&&  而是and
        DatabaseAccess db = new DatabaseAccess();
        db.query(sql);
        boolean b = db.isResultSetNull();
        db.close();
        return b;
    }

    public boolean check(User user) {
        String sql = "select * from user where username='" + user.getUsername() + "'";
        DatabaseAccess db = new DatabaseAccess();
        db.query(sql);
        boolean b = db.isResultSetNull();
        db.close();
        return b;

    }
}
